/**
 * LinkedLST, Gary Kuo, Kellen Donohue
 * May 13, 2011
 * LSTTester.java - application flow testing for Lab Specimen Transport application 
 * 
 * Due to physical and technical constraints, BarcodeView and BarcodeLookupView 
 * must be manually tested. Because of this, ReviewView must also be manually 
 * tested. Likewise for tasks involving updates to the database including:
 * 		- marking items as picked up ("Pickup" in ReviewView)
 * 		- marking items as missing ("Mark As Missing" context menu option in ClientView)
 * 		- marking stops as visited ("Next Location" in ClientView)
 */

package edu.washington.cs.labspecimentransport.test;

import android.test.ActivityInstrumentationTestCase2;
import android.test.FlakyTest;

import com.jayway.android.robotium.solo.Solo;

import edu.washington.cs.labspecimentransport.LoginView;
import edu.washington.cs.labspecimentransport.ops.Settings;

public class LSTTester extends ActivityInstrumentationTestCase2<LoginView> {

	private Solo solo;
	
	/**
	 * Modify the following variables to reflect current state of the app
	 * (other values may and will break the test)
	 */
	
	// an unvisited location in LocationView
	private final String firstUnvisited = "Harborview Medical Center";
	
	// an item shown in ClientView
	private final String firstItem = "AeRiYsCbHopUO";
	
	// a container shown in ClientView
	private final String firstBag = "51vQN025FrAuU";
	
	public LSTTester() {
		super("edu.washington.cs.labspecimentransport", LoginView.class);
	}
	
	@Override
	public void setUp() {
		Settings.testMode = true;
		solo = new Solo(getInstrumentation(), getActivity());
	}

	/** Go through all activities and make sure things aren't breaking **/
	@FlakyTest(tolerance=3)
	public void testGeneralFlow() {
		loginTest();
		
		// ======= CHECKLISTVIEW ==========
		
		solo.clickOnButton("Checklist");
		assertTrue(solo.searchText("Checklist"));
		
		for (int i = 0; i < solo.getCurrentCheckBoxes().size(); i++) {
			solo.clickOnCheckBox(i);
		}
		solo.clickOnButton("Save");
		assertTrue(solo.searchText("Home"));
		
		// ======= LOCATIONVIEW ===========
		
		solo.clickOnButton("Route");
		assertTrue(solo.searchText("Route Stops"));
		
		solo.clickOnText(firstUnvisited);
		solo.waitForDialogToClose(1000*60*2);
		
		// ======== CLIENTVIEW ============
		
		assertTrue(solo.searchText("SCAN ITEMS"));
		
		// ======== ITEMVIEW ==============
		
		// item lookup
		solo.clickLongOnTextAndPress(firstItem, 2);
		solo.clickLongOnTextAndPress(firstItem, 0);
		solo.waitForDialogToClose(1000*60*2);
		assertTrue(solo.searchText("INFO"));
		solo.clickOnText("COMMENTS");
		solo.goBack();
		assertTrue(solo.searchText("SCAN ITEMS"));
		
		// container lookup
		solo.clickLongOnTextAndPress(firstBag,2);
		solo.clickLongOnTextAndPress(firstBag,0);
		solo.waitForDialogToClose(1000*60*2);
		assertTrue(solo.searchText("INFO"));
		solo.clickOnText("COMMENTS");
		solo.clickOnText("INSIDE");
		solo.goBack();
		assertTrue(solo.searchText("SCAN ITEMS"));
	}
	
	// Refresh in LoginView
	@FlakyTest(tolerance=3)
	public void testLoginMenu() {
		solo.pressMenuItem(0);
	}
	
	// Refresh in HomeView
	@FlakyTest(tolerance=3)
	public void testHomeMenu() {
		loginTest();
		solo.pressMenuItem(0);
	}
	
	// Refresh in LocationsView
	@FlakyTest(tolerance=3)
	public void testLocationMenu() {
		loginTest();
		solo.clickOnButton("Route");
		solo.pressMenuItem(0);
		solo.waitForDialogToClose(1000*60*2);
	}
	
	// Refresh in ChecklistView
	@FlakyTest(tolerance=3)
	public void testChecklistMenu() {
		loginTest();
		solo.clickOnButton("Checklist");
		solo.pressMenuItem(0);
	}
	
	// Refresh in ClientView
	@FlakyTest(tolerance=3)
	public void testClientMenu() {
		loginTest();
		solo.clickOnButton("Route");
		solo.clickOnText(firstUnvisited);
		solo.waitForDialogToClose(1000*60*2);
		solo.pressMenuItem(0);
		solo.waitForDialogToClose(1000*60*2);
	}
	
	// Refresh in ItemView
	@FlakyTest(tolerance=3)
	public void testItemMenu() {
		loginTest();
		solo.clickOnButton("Route");
		solo.clickOnText(firstUnvisited);
		solo.waitForDialogToClose(1000*60*2);
		solo.clickLongOnTextAndPress(firstItem, 0);
		solo.waitForDialogToClose(1000*60*2);
		solo.pressMenuItem(0);
		solo.waitForDialogToClose(1000*60*2);
	}
	
	// "Home" menu options
	@FlakyTest(tolerance=3)
	public void testReturnHomeViaOptions() {
		loginTest();
		solo.clickOnButton("Checklist");
		solo.waitForDialogToClose(1000*60*2);
		solo.pressMenuItem(1);
		assertTrue(solo.searchText("Home"));
		
		solo.clickOnButton("Route");
		solo.waitForDialogToClose(1000*60*2);
		solo.pressMenuItem(1);
		assertTrue(solo.searchText("Home"));
		
		solo.clickOnButton("Route");
		solo.waitForDialogToClose(1000*60*2);
		solo.clickOnText(firstUnvisited);
		solo.waitForDialogToClose(1000*60*2);
		solo.pressMenuItem(1);
		assertTrue(solo.searchText("Home"));
		
		solo.clickOnButton("Route");
		solo.waitForDialogToClose(1000*60*2);
		solo.clickOnText(firstUnvisited);
		solo.waitForDialogToClose(1000*60*2);
		solo.clickLongOnTextAndPress(firstItem, 0);
		solo.waitForDialogToClose(1000*60*2);
		solo.pressMenuItem(1);
		assertTrue(solo.searchText("Home"));
	}
	
	public void loginTest() {
		solo.clickOnButton("Login");
		assertTrue(solo.searchText("Home"));
		solo.waitForDialogToClose(5*1000);
	}

	@Override
	public void tearDown() throws Exception {
		try {
			solo.finalize();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		
		getActivity().finish();
		super.tearDown();
	}

	
	
}
